package com.aiprompt.demos.controller;

import com.aiprompt.demos.common.BaseResponse;
import com.aiprompt.demos.common.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统监控接口
 * 提供线程池状态监控等功能
 */
@Api(tags = "系统监控")
@RestController
@RequestMapping("/monitor")
public class MonitorController {
    
    @Resource
    @Qualifier("aiTaskExecutor")
    private ThreadPoolTaskExecutor aiTaskExecutor;
    
    /**
     * 获取AI任务线程池状态
     */
    @ApiOperation("获取AI任务线程池状态")
    @GetMapping("/thread-pool/status")
    public BaseResponse<Map<String, Object>> getThreadPoolStatus() {
        Map<String, Object> status = new HashMap<>();
        
        // 当前活跃线程数
        status.put("activeCount", aiTaskExecutor.getActiveCount());
        
        // 当前线程池大小
        status.put("poolSize", aiTaskExecutor.getPoolSize());
        
        // 核心线程数
        status.put("corePoolSize", aiTaskExecutor.getCorePoolSize());
        
        // 最大线程数
        status.put("maxPoolSize", aiTaskExecutor.getMaxPoolSize());
        
        // 队列中等待的任务数
        status.put("queueSize", aiTaskExecutor.getThreadPoolExecutor().getQueue().size());
        
        // 队列容量
        int queueCapacity = aiTaskExecutor.getThreadPoolExecutor().getQueue().remainingCapacity() + 
                           aiTaskExecutor.getThreadPoolExecutor().getQueue().size();
        status.put("queueCapacity", queueCapacity);
        
        // 已完成的任务数
        status.put("completedTaskCount", aiTaskExecutor.getThreadPoolExecutor().getCompletedTaskCount());
        
        // 总任务数
        status.put("taskCount", aiTaskExecutor.getThreadPoolExecutor().getTaskCount());
        
        // 线程池状态
        status.put("isShutdown", aiTaskExecutor.getThreadPoolExecutor().isShutdown());
        status.put("isTerminated", aiTaskExecutor.getThreadPoolExecutor().isTerminated());
        
        // 计算使用率
        double poolUsage = (double) aiTaskExecutor.getActiveCount() / aiTaskExecutor.getMaxPoolSize() * 100;
        status.put("poolUsagePercent", Math.round(poolUsage * 100.0) / 100.0);
        
        double queueUsage = (double) aiTaskExecutor.getThreadPoolExecutor().getQueue().size() / queueCapacity * 100;
        status.put("queueUsagePercent", Math.round(queueUsage * 100.0) / 100.0);
        
        return ResultUtils.success(status);
    }
    
    /**
     * 获取系统运行时信息
     */
    @ApiOperation("获取系统运行时信息")
    @GetMapping("/runtime/info")
    public BaseResponse<Map<String, Object>> getRuntimeInfo() {
        Map<String, Object> info = new HashMap<>();
        
        Runtime runtime = Runtime.getRuntime();
        
        // 内存信息
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        long usedMemory = totalMemory - freeMemory;
        long maxMemory = runtime.maxMemory();
        
        info.put("totalMemoryMB", totalMemory / 1024 / 1024);
        info.put("freeMemoryMB", freeMemory / 1024 / 1024);
        info.put("usedMemoryMB", usedMemory / 1024 / 1024);
        info.put("maxMemoryMB", maxMemory / 1024 / 1024);
        info.put("memoryUsagePercent", Math.round((double) usedMemory / maxMemory * 100 * 100.0) / 100.0);
        
        // CPU信息
        info.put("availableProcessors", runtime.availableProcessors());
        
        // JVM信息
        info.put("javaVersion", System.getProperty("java.version"));
        info.put("jvmName", System.getProperty("java.vm.name"));
        
        return ResultUtils.success(info);
    }
}
